package it.unipd.fbaron.mind_reader_squared.activities;

import it.unipd.fbaron.mind_reader_squared.R;
import it.unipd.fbaron.mind_reader_squared.model.DataBaseHelper;
import it.unipd.fbaron.mind_reader_squared.model.SymbolDAO;
import it.unipd.fbaron.mind_reader_squared.model.SymbolLogic;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SummaryFragment extends Fragment {

	/**
	 * Create a new instance of DetailsFragment, initialized to show the text at
	 * 'index'.
	 */
	public static SummaryFragment newInstance(int profile, int mod) {
		SummaryFragment f = new SummaryFragment();

		Bundle args = new Bundle();
		args.putInt("profile", profile);
		args.putInt("mod", mod);
		f.setArguments(args);

		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		// Inflate the layout for this fragment
		View v = inflater.inflate(R.layout.summary_fragment_layout, container,
				false);

		TextView title = (TextView) v.findViewById(R.id.summary_title);
		ArrayList<SymbolDAO> symbolList = new ArrayList<SymbolDAO>();
		if (getArguments().getInt("mod") == DataBaseHelper.M1) {
			symbolList = SymbolLogic.getInstance(
					((ManageSymbolActivity) getActivity()).dbHelper)
					.getM1SymbolList(getArguments().getInt("profile"));
			title.setText(getString(R.string.m1) + " "
					+ getString(R.string.summary_title));
		} else if (getArguments().getInt("mod") == DataBaseHelper.M2) {
			symbolList = SymbolLogic.getInstance(
					((ManageSymbolActivity) getActivity()).dbHelper)
					.getM2SymbolList(getArguments().getInt("profile"), 0);
			title.setText(getString(R.string.m2) + " "
					+ getString(R.string.summary_title));
		}

		Collections
				.sort(symbolList, new SymbolLogic.SymbolComparatorClicksM1());

		LinearLayout list = (LinearLayout) v.findViewById(R.id.summary_list);
		int index = 3;
		int sum = 0;
		for (int i = symbolList.size() - 1; i >= 0; i--) {
			SymbolDAO symbol = symbolList.get(i);
			int nClicks = 0;
			if (getArguments().getInt("mod") == DataBaseHelper.M1) {
				nClicks = symbol.getClicksM1();
			} else if (getArguments().getInt("mod") == DataBaseHelper.M2) {
				nClicks = symbol.getClicksM2();
			}

			LinearLayout layout = new LinearLayout(getActivity());
			TextView text = new TextView(getActivity());
			TextView clicks = new TextView(getActivity());
			TextView hRow = new TextView(getActivity());

			layout.setLayoutParams(new LinearLayout.LayoutParams(500,
					LayoutParams.WRAP_CONTENT));
			layout.setOrientation(LinearLayout.HORIZONTAL);

			text.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			text.setGravity(Gravity.LEFT);
			text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
			text.setText(symbol.getText());

			clicks.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			clicks.setGravity(Gravity.RIGHT);
			clicks.setTextSize(TypedValue.COMPLEX_UNIT_SP, 35);
			clicks.setText(String.valueOf(nClicks));
			sum += nClicks;

			hRow.setLayoutParams(new LinearLayout.LayoutParams(500, 1));
			hRow.setBackgroundColor(Color.BLACK);

			layout.addView(text);
			layout.addView(clicks);

			list.addView(layout, index++);
			list.addView(hRow, index++);
		}

		TextView total = (TextView) v.findViewById(R.id.summary_total);
		total.setText(String.valueOf(sum));

		return v;
	}
}
